<template>
	<view class="pageView">
		<u-loading-page :loading="addPeopleLoading" bg-color="#e8e8e8"></u-loading-page>
		<u-navbar
			title="添加就诊人"
			@leftClick="handleLeftClick"
		>
		</u-navbar>
		<view class="formBlock">
			<u--form
				labelPosition="left"
				:model="form"
				:rules="rules"
				ref="uForm"
				labelWidth="100"
			>
				<u-form-item
					label="姓名"
					prop="name"
					borderBottom
					ref="item1"
				>
					<u--input
						v-model="form.name"
						border="none"
						placeholder="请输入姓名"
					></u--input>
				</u-form-item>
				<u-form-item
					label="性别"
					prop="sex"
					borderBottom
					ref="item1"
				>
					<u-radio-group
					  v-model="form.sex"
						placement="row"
					>
						<u-radio
							:customStyle="{marginRight: '32px'}"
							v-for="(item, index) in radiolist"
							:key="index"
							:label="item.name"
							:name="item.value"
						>
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item
					label="年龄(周岁)"
					prop="age"
					borderBottom
					ref="item1"
				>
					<u--input
						v-model="form.age"
						border="none"
						placeholder="请输入就诊人年龄..."
					></u--input>
				</u-form-item>
				<u-form-item
					label="电话"
					prop="phone"
					borderBottom
					ref="item1"
				>
					<u--input
						v-model="form.phone"
						border="none"
						placeholder="请输入就诊人电话..."
					></u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="buttonBlock">
			<button @click="handleEditPeople" v-if="peopleId">保存</button>
			<button @click="handleAddPeople" v-else>添加</button>
		</view>
	</view>
</template>

<script>
	import {
		partnerAdd
	} from '../../api/login.js'
	export default{
		data() {
			return {
				addPeopleLoading: false,
				peopleId: '',
				form: {
					name: '',
					sex: '1',
					age: '',
					phone: ''
				},
				rules: {
					'form.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'form.sex': {
						type: 'string',
						required: true,
						message: '请选择性别',
						trigger: ['blur', 'change']
					},
					'form.age': {
						type: 'string',
						required: true,
						message: '请填写年龄',
						trigger: ['blur', 'change']
					},
					'form.phone': {
						type: 'string',
						required: true,
						message: '请填写电话',
						trigger: ['blur', 'change']
					}
				},
				radiolist: [
					{
						name: '男',
						value: '1'
					},
					{
						name: '女',
						value: '0'
					}
				]
			}
		},
		onShow() {
			this.form.name = uni.getStorageSync('peopleEdit').name
			this.form.sex = uni.getStorageSync('peopleEdit').gender.toString()
			this.form.phone = uni.getStorageSync('peopleEdit').mobile
			this.form.age = uni.getStorageSync('peopleEdit').age
			this.peopleId = uni.getStorageSync('peopleEdit').id
		},
		methods: {
			handleAddPeople() {
				this.addPeopleLoading = true
				let data = {
					name: this.form.name,
					gender: this.form.sex,
					age: this.form.age,
					mobile: this.form.phone
				}
				partnerAdd(data).then(res => {
					const { code } = res
					if (code == 200001) {
						this.addPeopleLoading = false
						uni.showToast({
							title: '创建成功',
							icon:'none'
						})
						setTimeout(function() {
							uni.navigateTo({
								url: '/pages/peopleList/peopleList'
							})
						}, 2000)
					} else {
						this.addPeopleLoading = false
					}
				})
				.catch(e => {
					this.addPeopleLoading = false
				})
			},
			handleEditPeople() {
				this.addPeopleLoading = true
				let data = {
					name: this.form.name,
					gender: this.form.sex,
					age: this.form.age,
					mobile: this.form.phone,
					id: this.peopleId
				}
				partnerAdd(data).then(res => {
					const { code } = res
					if (code == 200001) {
						this.addPeopleLoading = false
						uni.showToast({
							title: '修改成功',
							icon:'none'
						})
						uni.removeStorageSync('peopleEdit')
						setTimeout(function() {
							uni.navigateTo({
								url: '/pages/peopleList/peopleList'
							})
						}, 2000)
					} else {
						this.addPeopleLoading = false
					}
				})
				.catch(e => {
					this.addPeopleLoading = false
				})
			},
			handleLeftClick() {
				uni.removeStorageSync('peopleEdit')
				uni.navigateTo({
					url: '/pages/peopleList/peopleList'
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.pageView{
		height: 100vh;
		background: #F4F4F4;
		.formBlock{
			margin-top: 176rpx;
			padding: 32rpx;
			background: #fff;
		}
		.buttonBlock{
			padding: 32rpx;
			button{
				display: flex;
				justify-content: center;
				align-items: center;
				width: 688rpx;
				height: 78rpx;
				background: #0165FB;
				border-radius: 68rpx 68rpx 68rpx 68rpx;
				font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
				font-weight: normal;
				font-size: 30rpx;
				color: #FFFFFF;
			}
		}
	}
</style>